package dk.pun.dums.client.presenter;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;

import dk.pun.dums.client.AnonymousLogin;
import dk.pun.dums.client.view.TopBarView;

public class TopBarPresenter implements Presenter, TopBarView.Presenter {

	private final transient TopBarView view;
	private transient String signInURL;
	private transient String signOutURL;
	private transient boolean isAnonymousUse;

	public TopBarPresenter(final TopBarView view) {
		this.view = view;
		if (this.view != null) {
			this.view.setPresenter(this);
		}
	}

	public void setSignedOut(final String signInURL) {
		this.signInURL = signInURL;
		this.view.setSignedOut();
	}
	
	public void setSignedIn(final String signOutURL, final boolean isAnonymousUse, final String userNickname) {
		this.signOutURL = signOutURL;
		this.isAnonymousUse = isAnonymousUse;
		this.view.setSignedIn(userNickname);
	}
	
	@Override
	public void onAnonymousUse() {
		AnonymousLogin.login();
		Window.Location.assign("/");
	}

	@Override
	public void onSignIn() {
		if (this.signInURL != null) {
			Window.Location.assign(this.signInURL);
		}
	}

	@Override
	public void onSignOut() {
		if (this.isAnonymousUse) {
			AnonymousLogin.logout();
		}
		Window.Location.assign(this.signOutURL);
	}

	@Override
	public void onUsername() {
		// TODO Not implemented yet
	}

	@Override
	public void go(final HasWidgets container) {
		container.clear();
		container.add(this.view.asWidget());
	}

}
